<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

require_once("matches.php");

class Highscore extends MY_Controller {

	private $points_perfect;
	private $points_difference;
	private $points_tendency;
	
	private $now; 

	private $selections;
	
    function Highscore() {
    	
        parent::MY_Controller();

		$this->now = date("Y-m-d H:i:s");
        
        $this->ci =& get_instance();
		$this->points_perfect    = $this->ci->config->item('points_perfect');
		$this->points_difference = $this->ci->config->item('points_difference');
		$this->points_tendency   = $this->ci->config->item('points_tendency');
		
		$sql = "SELECT DISTINCT matchgroup as name FROM ".$this->tblm;
		$query = $this->db->query($sql);
		$this->selections = Matches::getGroups($query->result());

    }
	
	function index() {
		$data = $this->getData('highscore');

		$sql = "SELECT "
			." tu.id AS id,"
			." tu.settings AS settings,"
			." tu.name AS name,"
			." sum(tp.tendency) as tendency,"
			." sum(tp.difference) AS difference,"
			." sum(tp.perfect) AS perfect,"
			." sum(tp.wrong) AS wrong,"
			." sum(tp.extra) AS extra,"
				// punkte
				."( sum(tp.tendency)*".$this->points_tendency
				."+ sum(tp.difference)*".$this->points_difference
				."+ sum(tp.perfect)*".$this->points_perfect
				."+ sum(tp.extra))"
				." AS points"
			." FROM ".$this->tblu." AS tu, ".$this->tblp." AS tp"
			." WHERE tu.id = tp.user_id"
			." GROUP BY id"
			." ORDER BY points DESC, tendency DESC";

		$query = $this->db->query($sql);
    	$d['user'] = $query->result();
    	$d['userdata'] = $this->userdata;
    	
		$d['selection'] = 'all';
		$d['selections'] = $this->selections;

		$data["content"] = $this->load->view('highscore', $d, true);
		$this->load->view('layout', $data);
	}
	
	function bestof() {
    	$data = $this->getData('highscore');

		$sql = "CREATE TEMPORARY TABLE IF NOT EXISTS temp_select SELECT"
			." tu.id AS id,"
			." tu.name AS name,"
			." tu.settings AS settings,"
			." tp.tendency AS tendency,"
			." tp.difference AS difference,"
			." tp.perfect AS perfect,"
			." tp.wrong AS wrong,"
			." tp.extra AS extra,"
			." tp.matchgroup AS matchgroup,"			
				// punkte
				."( tp.tendency*".$this->points_tendency
				."+ tp.difference*".$this->points_difference
				."+ tp.perfect*".$this->points_perfect
				."+ tp.extra)"
				." AS points"
			." FROM ".$this->tblu." AS tu, ".$this->tblp." AS tp"
			." WHERE tu.id = tp.user_id"
			." ORDER BY points DESC, tendency DESC";

		$query = $this->db->query($sql);

		$sql = "SELECT *"
			." FROM temp_select"
			." GROUP BY id"
			." ORDER BY points DESC, tendency DESC";
		
		$query = $this->db->query($sql);
    	$d['user'] = $query->result();
    	$d['userdata'] = $this->userdata;

		$d['selection'] = 'bestof';
		$d['selections'] = $this->selections;

		$query = $this->db->query("DROP TEMPORARY TABLE IF EXISTS temp_select");
		$query = $this->db->query("COMMIT;");	


		$data["content"] = $this->load->view('highscore', $d, true);
		$this->load->view('layout', $data);
	}

	function day($number) {
		$data = $this->getData('highscore');

		$number = intval("".$number);
		
		$sql = "SELECT "
			." tu.id AS id,"
			." tu.name AS name,"
			." tu.settings AS settings,"
			." tp.tendency AS tendency,"
			." tp.difference AS difference,"
			." tp.perfect AS perfect,"
			." tp.wrong AS wrong,"
			." tp.extra AS extra,"
				// punkte
				."( tp.tendency*".$this->points_tendency
				."+ tp.difference*".$this->points_difference
				."+ tp.perfect*".$this->points_perfect
				."+ tp.extra)"
				." AS points"
			." FROM ".$this->tblu." AS tu, ".$this->tblp." AS tp"
			." WHERE tu.id = tp.user_id"
			." AND tp.matchgroup = ?"
			." ORDER BY points DESC, tendency DESC";

		$query = $this->db->query($sql, array($number));
    	$d['user'] = $query->result();
    	$d['userdata'] = $this->userdata;

		$d['selection']  = $number;
		$d['selections'] = $this->selections;

		$data["content"] = $this->load->view('highscore', $d, true);
		$this->load->view('layout', $data);
	}
	
	function currentday() {

		$sql = "SELECT matchgroup FROM ".$this->tblm." WHERE deadline < ? ORDER BY deadline DESC LIMIT 1";
		$query = $this->db->query($sql, array($this->now));

		if (!$query->num_rows() > 0) {
			$sql = "SELECT matchgroup FROM ".$this->tblm." ORDER BY deadline ASC LIMIT 1";
			$query = $this->db->query($sql, array($this->now));
		}

		$res = $query->result();
		$group = $res[0]->matchgroup;

		redirect("highscore/day/".$group);
	}
	
	function selection() {
		$s = $this->input->post('selection');
		redirect($s);
	}
	
}
?>
